package com.apestech.framework.sql.variable;

import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.modul.VarPos;
import com.apestech.framework.xml.sql.SqlNodeVar;
import com.apestech.framework.stream.ArrayObject;

/**
 * @author hup
 * @create 2018-08-27-11:34
 */
public class SqlInfo {

    private String execSql;
    private String printSql;
    private SqlSingleValue singleValue;
    private JSONArray paramList;

    public SqlInfo(String dbType, String sqlType){
        singleValue=new SqlSingleValue(dbType, sqlType);
    }

    public String getExecSql() {
        return execSql;
    }

    public String getPrintSql() {
        return printSql;
    }

    public void setExecSql(String execSql) {
        this.execSql = execSql;
    }

    public void setPrintSql(String printSql) {
        this.printSql = printSql;
    }

    public JSONArray getParamList() {
        return paramList;
    }

    public void setParamList(JSONArray paramList) {
        this.paramList = paramList;
    }

    public void expain(String sqltext, VarPos var, boolean last) throws Exception {
        String sqlValue="";
        String printValue="";
        if("SYSDATE".equals(var.getVar()) || "SYSDATE".equals(var.getValue())){
            var.setSymbol(SqlNodeVar.getValueVar());
            var.setValue("SYSDATE");
        }
        if(SqlNodeVar.getValueVar().equals(var.getSymbol())){
            if(var.getValue()!=null){
                if(var.getVar().indexOf(SqlNodeVar.getLISTVAR())>=0){
                    int pos=var.getVar().lastIndexOf(SqlNodeVar.getLISTVAR());
                    if(pos<0){
                        throw new Exception("变量配置有问题，请检查SQL语句的写法准确性！");
                    }
                    int index=Integer.valueOf(var.getVar().substring(pos+1, var.getVar().length()));
                    singleValue.setVar(var.getTab(), var.getVar(), new ArrayObject(var.getValue()).getText(index), var.getFieldType(), var.getFieldValue(), var.getNotNull());
                } else {
                    singleValue.setVar(var.getTab(), var.getVar(), var.getValue(), var.getFieldType(), var.getFieldValue(), var.getNotNull());
                }
                singleValue.expain();
                sqlValue=singleValue.getResultPrintValue();
                printValue=sqlValue;
            }
        } else {
            singleValue.setVar(var.getTab(), var.getVar(), var.getValue(), var.getFieldType(), var.getFieldValue(), var.getNotNull());
            singleValue.expain();
            Object paramValue=singleValue.getResultValue();
            String paramType=singleValue.getResultType();
            sqlValue="?";
            printValue=singleValue.getResultPrintValue();
            if(paramType==null) paramType=var.getFieldType();
            if(paramType==null) paramType=singleValue.getVarType();
            if(paramType==null && paramValue==null){
                throw new Exception("传入参数["+var.getVar()+"]有问题，请检查！");
            }
            addParam(paramValue, paramType);
        }
        int len=sqltext.length();
        String st="";
        int lastEnd=var.getLastEnd();
        if(var.getStart()>0){
            if(lastEnd==0){
                lastEnd=-1;
            }
            st=sqltext.substring(lastEnd+1, var.getStart());
        }
        execSql=execSql+st+sqlValue;
        printSql=printSql+st+printValue;
        if(last){
            String end=sqltext.substring(var.getEnd()+1, len);
            execSql=execSql+end;
            printSql=printSql+end;
        }
    }

    private void addParam(Object value, String type){
        JSONObject pmap=new JSONObject();
        pmap.put("value", value);
        pmap.put("type", type);
        if(pmap.get("value")==null){
            pmap.put("value","null");
        }
        this.paramList.add(pmap);
    }

}
